home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / cmap.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  207 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    cmap -
  19.  *        Color map support
  20.  *
  21.  *            Paul Haeberli - 1990
  22.  *
  23.  *    exports
  24.  *
  25.     cmap *newcmap(rbuf,gbuf,bbuf,abuf,ncolors)
  26.     void freecmap(map)
  27.     void writecmap(name,map)
  28.     cmap *readcmap(name)
  29.     int colordist(r0,g0,b0,r1,g1,b1)
  30.     int cmapcolor(map,c)
  31.     void cmapsample(map,ci,c)
  32.     void printcmap(map)
  33.  *
  34.  */
  35. #include "image.h"
  36. #include "vect.h"
  37. #include "cmap.h"
  38.  
  39. cmap *newcmap(rbuf,gbuf,bbuf,abuf,ncolors)
  40. short *rbuf, *gbuf, *bbuf, *abuf;
  41. int ncolors;
  42. {
  43.     cmap *map;
  44.  
  45.     map = (cmap *)mymalloc(sizeof(cmap));
  46.     map->ncolors = ncolors;
  47.     map->r = (short *)mymalloc(ncolors*sizeof(short));
  48.     bcopy(rbuf,map->r,ncolors*sizeof(short));
  49.     map->g = (short *)mymalloc(ncolors*sizeof(short));
  50.     bcopy(gbuf,map->g,ncolors*sizeof(short));
  51.     map->b = (short *)mymalloc(ncolors*sizeof(short));
  52.     bcopy(bbuf,map->b,ncolors*sizeof(short));
  53.     if(abuf) {
  54.     map->nchans = 4;
  55.     map->a = (short *)mymalloc(ncolors*sizeof(short));
  56.     bcopy(abuf,map->a,ncolors*sizeof(short));
  57.     } else {
  58.     map->nchans = 3;
  59.     map->a = 0;
  60.     }
  61.     return map;
  62. }
  63.  
  64. void freecmap(map)
  65. cmap *map;
  66. {
  67.     free(map->r);
  68.     free(map->g);
  69.     free(map->b);
  70.     if(map->nchans>3)
  71.     free(map->a);
  72.     free(map);
  73. }
  74.  
  75. void writecmap(name,map)
  76. char *name;
  77. cmap *map;
  78. {
  79.     IMAGE *image;
  80.  
  81.     image=iopen(name,"w",RLE(1),3,map->ncolors,1,map->nchans);
  82.     putrow(image,map->r,0,0);
  83.     putrow(image,map->g,0,1);
  84.     putrow(image,map->b,0,2);
  85.     if(map->nchans>3)
  86.     putrow(image,map->a,0,3);
  87.     iclose(image);
  88. }
  89.  
  90. cmap *readcmap(name)
  91. char *name;
  92. {
  93.     IMAGE *image;
  94.     cmap *map;
  95.     int ncolors;
  96.  
  97.     map = (cmap *)mymalloc(sizeof(cmap));
  98.     image=iopen(name,"r");
  99.     if(!image) {
  100.     fprintf(stderr,"readcmap: can't open input file %s\n",name);
  101.     exit(1);
  102.     }
  103.     ncolors = image->xsize;
  104.     map->ncolors = ncolors;
  105.     map->nchans = image->zsize;
  106.     map->r = (short *)mymalloc(ncolors*sizeof(short));
  107.     getrow(image,map->r,0,0%image->zsize);
  108.     map->g = (short *)mymalloc(ncolors*sizeof(short));
  109.     getrow(image,map->g,0,1%image->zsize);
  110.     map->b = (short *)mymalloc(ncolors*sizeof(short));
  111.     getrow(image,map->b,0,2%image->zsize);
  112.     if(image->zsize>3) {
  113.     map->a = (short *)mymalloc(ncolors*sizeof(short));
  114.     getrow(image,map->a,0,3%image->zsize);
  115.     }
  116.     iclose(image);
  117.     return map;
  118. }
  119.  
  120. int colordist(r0,g0,b0,r1,g1,b1)
  121. int r0,g0,b0,r1,g1,b1;
  122. {
  123.     int dist, dr, dg, db;
  124.  
  125.     dr = 100*(r0-r1);
  126.     dg = 100*(g0-g1);
  127.     db = 100*(b0-b1);
  128.     dist = 0;
  129.     if(dr<0) 
  130.     dist -= dr;
  131.     else
  132.     dist += dr;
  133.     if(dg<0) 
  134.     dist -= dg;
  135.     else 
  136.     dist += dg;
  137.     if(db<0) 
  138.     dist -= db;
  139.     else 
  140.     dist += db;
  141.     return dist;
  142. }
  143.  
  144. int cmapcolor(map,c)
  145. cmap *map;
  146. vect *c;
  147. {
  148.     short *rmap, *gmap, *bmap;
  149.     int rwant, gwant, bwant;
  150.     int i, min, dist, slot;
  151.  
  152.     rwant = c->x*255.0;
  153.     gwant = c->y*255.0;
  154.     bwant = c->z*255.0;
  155.     rmap = map->r;
  156.     gmap = map->g;
  157.     bmap = map->b;
  158.     min = 1000000;
  159.     for(i=0; i<map->ncolors; i++) {
  160.     dist = colordist(rwant,gwant,bwant,rmap[i],gmap[i],bmap[i]);
  161.     if(dist<min) {
  162.         min = dist;
  163.         slot = i;
  164.     }
  165.     }
  166.     c->x = rmap[slot]/255.0;
  167.     c->y = gmap[slot]/255.0;
  168.     c->z = bmap[slot]/255.0;
  169.     return slot;
  170. }
  171.  
  172. void cmapsample(map,ci,c)
  173. cmap *map;
  174. int ci;
  175. vect *c;
  176. {
  177.     if(ci<map->ncolors) {
  178.     c->x = map->r[ci]/255.0;
  179.     c->y = map->g[ci]/255.0;
  180.     c->z = map->b[ci]/255.0;
  181.     if(map->nchans>3)
  182.         c->z = map->a[ci]/255.0;
  183.     }
  184. }
  185.  
  186. void printcmap(map)
  187. cmap *map;
  188. {
  189.     int i;
  190.     short *rptr, *gptr, *bptr, *aptr;
  191.  
  192.     printf("cmap colors %d\n",map->ncolors);
  193.     rptr = map->r;
  194.     gptr = map->g;
  195.     bptr = map->b;
  196.     aptr = map->a;
  197.     if(map->nchans == 4) {
  198.       for(i=0; i<map->ncolors; i++) {
  199.         printf("pos %d: %3d %3d %3d %3d\n",i,*rptr++,*gptr++,*bptr++,*aptr++);
  200.     }
  201.     } else {
  202.       for(i=0; i<map->ncolors; i++) {
  203.         printf("pos %d: %3d %3d %3d\n",i,*rptr++,*gptr++,*bptr++);
  204.     }
  205.     }
  206. }
  207.